package scau.mis4.chanyulin.atm;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import scau.mis4.chanyulin.atm.base.common.CODE;
import scau.mis4.chanyulin.atm.base.dictionary.BankCompany;
import scau.mis4.chanyulin.atm.controller.ATM;
import scau.mis4.chanyulin.atm.domain.Card;
import scau.mis4.chanyulin.atm.view.*;
import scau.mis4.chanyulin.atm.view.AlertStage;
import java.util.Arrays;
import java.util.List;

/**
 * Created by chanyulin on 2018/4/27.
 * 程序入口
 */
public class APP extends Application {

    // 个人物品栏
    public static final List<Card> customerCardList = Arrays.asList(
            new Card("A0001", BankCompany.中国农业银行),
            new Card("A0002", BankCompany.中国农业银行),
            new Card("G0241", BankCompany.中国工商银行)
    );
    public static final int customerBalance = 1000;
    public static CustomerBagView customerBagView;

    // ATM系统
    public static final ATM ATM = new ATM();

    // 硬件图形
    public static LoggerView loggerView;
    public static CardReaderView cardReaderView;
    public static CashierView cashierView;
    public static ReceiptPrinterView receiptPrinterView;
    public static MonitorView monitorView;

    public static void main(String[] args) {
        // 开机
        ATM.powerOn();
        // 启动图形界面
        Application.launch(args);
    }

    @Override
    public void start(Stage atmStage) throws Exception {

        // 初始化用户物品栏
        customerBagView = new CustomerBagView(customerBalance, customerCardList);
        customerBagView.setPadding(new Insets(7.5, 0, 0, 7.5));

        // 初始化硬件图形
        loggerView = new LoggerView();
        cardReaderView = new CardReaderView();
        cashierView = new CashierView();
        receiptPrinterView = new ReceiptPrinterView();
        monitorView = new MonitorView();

        // 日志记录开机完成
        APP.loggerView.log("系统开机完成");

        // 初始化主界面
        VBox physicalBox = new VBox();
        physicalBox.getChildren().addAll(cardReaderView, cashierView, receiptPrinterView);

        HBox upperBox = new HBox(25);
        upperBox.getChildren().addAll(monitorView, physicalBox);
        upperBox.setPadding(new Insets(7.5, 0, 0, 7.5));

        Separator separator = new Separator();
        separator.setOrientation(Orientation.HORIZONTAL);
        separator.setPadding(new Insets(5, 0, 0, 0));

        VBox mainBox = new VBox();
        mainBox.getChildren().addAll(upperBox, separator, customerBagView);

        Group root = new Group();
        root.getChildren().add(mainBox);

        // 开机、关机菜单项
        MenuItem miPowerOn = new MenuItem("开机");
        MenuItem miPowerOff = new MenuItem("关机");
        miPowerOn.setOnAction(e -> {
            APP.ATM.powerOn();
            APP.monitorView.goToWelcomePage();
            miPowerOn.setDisable(true);
            miPowerOff.setDisable(false);
            APP.loggerView.log("系统开机完成");
        });
        miPowerOn.setDisable(true);
        miPowerOff.setOnAction(e -> {
            APP.loggerView.log("系统正在关机...");
            int resultCode = APP.ATM.powerOff();
            if (CODE.MSG_SUCCESS == resultCode) {
                APP.cardReaderView.setCardEjectedImg();
                APP.loggerView.log("成功退卡");
            }
            else
                APP.cardReaderView.setCardEmptyImg();
            APP.monitorView.goToEmptyPage();
            APP.loggerView.clearAllLog();
            miPowerOff.setDisable(true);
            miPowerOn.setDisable(false);
        });
        Menu mSys = new Menu("系统", null, miPowerOn, miPowerOff);
        MenuItem miManual = new MenuItem("使用手册");
        miManual.setOnAction(e -> {
            new AlertStage("使用手册",
                    "1.启动前，可通过“APP”类中的属性修改初始个人物品\n" +
                    "2.双击物品可查看物品的详情\n" +
                    "3.插卡时可将银行卡拖至读卡器\n" +
                    "4.存款时可将钞票拖至出钞机\n" +
                    "5.两张默认的农行卡密码分别为“123456”和“abcdefg”\n",
                    1.5, 1.0).show();
        });
        Menu mHelp = new Menu("帮助", null, miManual);
        MenuBar menuBar = new MenuBar(mSys, mHelp);

        VBox manageBox = new VBox();
        manageBox.getChildren().addAll(menuBar, loggerView);

        // 同时显示ATM前台和后台界面
        atmStage.setScene(new Scene(root));
        atmStage.setResizable(false);
        atmStage.setTitle("ATM Simulator");

        Stage manageStage = new Stage();
        manageStage.setScene(new Scene(manageBox));
        manageStage.heightProperty().addListener(e -> {
            loggerView.setPrefHeight(manageStage.getHeight());
        });
        manageStage.setTitle("ATM Manager");

        manageStage.show();
        atmStage.show();
    }

}
